package UPPM;

import java.util.ResourceBundle;

import java.util.concurrent.TimeUnit;

import junit.framework.Assert;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import UPPM.Utils;

public class TestHomepage {

	WebDriver driver = new FirefoxDriver();

	@Before
	public void setUp() throws Exception {
		driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
		driver.manage().window().maximize();
		driver.get("http://test.unippmtest.co.uk");	
		
		}

	@Test
	public void testHomePageInitialStateNotLoggedIn() throws Exception {
		
		//check top page - right buttons
		WebElement helpPanel = driver.findElement(By.id("global_help_btn"));
		Assert.assertEquals("Help Panels:off", helpPanel.getText());
		
		WebElement createAccount = driver.findElement(By.className("navigation"));
		System.out.println(createAccount.getText());
		Assert.assertEquals("Create account", createAccount.getText().trim());
		
		WebElement loginBtn = driver.findElement(By.id("login"));
		System.out.println(loginBtn.getText());
		Assert.assertEquals("Login", loginBtn.getText().trim());
		
		//check tabs - user not logged in
		
//		WebElement search = driver.findElement(By.xpath(".//*[@id='quick_search_btn']"));
//		Assert.assertEquals("Search", search.getText());
		
		Thread.sleep(1000);
		Utils.waitForElement(driver, By.linkText("Browse Music"));
		WebElement browse_libraries = driver.findElement(By.linkText("Browse Music"));
		System.out.println(browse_libraries.getText());
		Assert.assertEquals("Browse Music", browse_libraries.getText());
		Utils.waitForElement(driver, By.linkText("Playlists"));
		WebElement themed_playlists = driver.findElement(By.linkText("Playlists"));
		System.out.println(themed_playlists.getText());
		Assert.assertEquals("Playlists", themed_playlists.getText());

		//check the rest of the elements on the page
//		Assert.assertEquals(true, Utils.isElementPresent(driver, By.xpath("//*[@id='search_block']/div[1]")));
//		Assert.assertEquals(true, Utils.isElementPresent(driver, By.xpath("//*[@id='carousel']/div[1]/span")));
//		Assert.assertEquals(true, Utils.isElementPresent(driver, By.xpath("//*[@id='carousel']/div[2]/div")));
//		Assert.assertEquals(true, Utils.isElementPresent(driver, By.xpath("//*[@id='carousel']/div[3]/span")));
//		Assert.assertEquals(true, Utils.isElementPresent(driver, By.className("border-top")));
//		Assert.assertEquals(true, Utils.isElementPresent(driver, By.className("block480")));
			
		//check footer existence
		Assert.assertEquals(true, Utils.isElementPresent(driver, By.linkText("Contact Us")));
		Assert.assertEquals(true, Utils.isElementPresent(driver, By.linkText("UK Team & Contact Info")));
		Assert.assertEquals(true, Utils.isElementPresent(driver, By.linkText("Worldwide Offices")));
		Assert.assertEquals(true, Utils.isElementPresent(driver, By.linkText("Licensing")));
		Assert.assertEquals(true, Utils.isElementPresent(driver, By.linkText("Our Composers")));
		Assert.assertEquals(true, Utils.isElementPresent(driver, By.linkText("Company History")));
		Assert.assertEquals(true, Utils.isElementPresent(driver, By.linkText("Album Reviews")));
		
	}
	
	@Test
	public void testHomePageInitialStateUserLoggedIn() throws Exception {
		
		Utils.Login(driver);
		
		
		WebElement helpPanel = driver.findElement(By.id("global_help_btn"));
		Assert.assertEquals("Help Panels:off", helpPanel.getText());
		Assert.assertEquals(true, Utils.isElementPresent(driver, By.id("logout")));
		WebElement download_btn = driver.findElement(By.id("yourDownloads"));
		System.out.println(download_btn.getText());
		Assert.assertEquals(true, Utils.isElementPresent(driver, By.id("yourDownloads")));
		
		//check tabs - user  logged in
		WebElement projects = driver.findElement(By.xpath(".//*[@id='sub-nav-menu']/li[4]/a"));
		Assert.assertEquals("Projects", projects.getText());
		WebElement music_for = driver.findElement(By.linkText("Music For"));
		Assert.assertEquals("Music For", music_for.getText().trim());

		//check the rest of the elements on the page
		Assert.assertEquals(true, Utils.isElementPresent(driver, By.xpath(".//*[@id='pageRows']/div/div[1]/div[3]/div[2]/a/div[2]")));
		Assert.assertEquals(true, Utils.isElementPresent(driver, By.xpath(".//*[@id='carousel']/div[2]/div")));
		Assert.assertEquals(true, Utils.isElementPresent(driver, By.xpath(".//*[@id='pageRows']/div/div[1]/div[5]/div[2]/a/div[2]")));
		Assert.assertEquals(true, Utils.isElementPresent(driver, By.xpath(".//*[@id='pageRows']/div/div[1]/div[7]/div/div[2]/div")));
				
			
		//check footer existence and links
		Assert.assertEquals(true, Utils.isElementPresent(driver, By.linkText("Contact Us")));
		Assert.assertEquals(true, Utils.isElementPresent(driver, By.linkText("UK Team & Contact Info")));
		Assert.assertEquals(true, Utils.isElementPresent(driver, By.linkText("Worldwide Offices")));
		Assert.assertEquals(true, Utils.isElementPresent(driver, By.linkText("Latest News")));
		Assert.assertEquals(true, Utils.isElementPresent(driver, By.linkText("Licensing")));
		Assert.assertEquals(true, Utils.isElementPresent(driver, By.linkText("Our Composers")));
		Assert.assertEquals(true, Utils.isElementPresent(driver, By.linkText("Company History")));
		Assert.assertEquals(true, Utils.isElementPresent(driver, By.linkText("Album Reviews")));
		
		}
	
//	@Test
//	public void testHomePageInitialStateUserLoggedInAdminUser() throws Exception {
//	
//		Utils.CompanyAdminUserLogin(driver);
//		
//		WebElement helpPanel = driver.findElement(By.id("global_help_btn"));
//		Assert.assertEquals("Turn on help panels", helpPanel.getText());
//		Assert.assertEquals(true, Utils.isElementPresent(driver, By.id("download_manager")));
//		Assert.assertEquals(true, Utils.isElementPresent(driver, By.linkText("Account Details")));
//		Assert.assertEquals(true, Utils.isElementPresent(driver, By.id("logout")));
//		
//		//check tabs - user not logged in
//		WebElement search = driver.findElement(By.xpath("//*[@id='outline']/div/div[2]/div[3]/div[2]/ul/li[1]/a/span"));
//		Assert.assertEquals("Search", search.getText());
//		WebElement browse_libraries = driver.findElement(By.xpath("//*[@id='outline']/div/div[2]/div[3]/div[2]/ul/li[2]/a/span"));
//		Assert.assertEquals("Browse Music", browse_libraries.getText());
//		WebElement projects = driver.findElement(By.xpath("/html/body/div/div/div/div[2]/div[3]/div[2]/ul/li[6]/a/span"));
//		Assert.assertEquals("Projects", projects.getText());
//		WebElement themed_playlists = driver.findElement(By.xpath("/html/body/div/div/div/div[2]/div[3]/div[2]/ul/li[3]/a/span"));
//		Assert.assertEquals("Themed Playlists", themed_playlists.getText());
//		WebElement admin = driver.findElement(By.linkText("ADMIN"));
//		Assert.assertEquals("ADMIN", admin.getText());
//
//		//check the rest of the elements on the page
//		Assert.assertEquals(true, Utils.isElementPresent(driver, By.xpath("//*[@id='search_block']/div[1]")));
//		Assert.assertEquals(true, Utils.isElementPresent(driver, By.xpath("//*[@id='carousel']/div[1]/span")));
//		Assert.assertEquals(true, Utils.isElementPresent(driver, By.xpath("//*[@id='carousel']/div[2]/div")));
//		Assert.assertEquals(true, Utils.isElementPresent(driver, By.xpath("//*[@id='carousel']/div[3]/span")));
//		Assert.assertEquals(true, Utils.isElementPresent(driver, By.className("border-top")));
//		Assert.assertEquals(true, Utils.isElementPresent(driver, By.className("block480")));
//			
//		//check footer existence
//		Assert.assertEquals(true, Utils.isElementPresent(driver, By.xpath("//*[@id='outline']/div/div[7]/div/div[2]/div")));
//				
//	}
	
	@After
	public void tearDown() throws Exception {
		System.out.println("******************************");
		driver.quit();
	}
}
